{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a94911f5",
   "metadata": {},
   "source": [
    "# 3. 本周作业：\n",
    "\n",
    "## 尝试创建detect.py 并实现调用功能\n",
    "## 尝试创建analyze.py\\campare.py\\search.py，并实现调用功能\n",
    "## 尝试存放宿舍4位同学的人脸信息到 人脸数据集\n",
    "完善faceset信息\n",
    "实现人脸搜索和人脸对比，打印结果：是本人or不是本人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7a8a46c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_key = \"iFunMsDgsjGWluC2dute5E7jpaBYrlVI\"\n",
    "API_sercret = \"A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "25acc6eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "53b60992",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect - # coding: utf-8\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    /Users/panyanfen/week_04人臉/detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9f500faf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648565658,471ec0f7-8eb3-4bdd-8c8d-93839a12bb75',\n",
       " 'time_used': 366,\n",
       " 'faces': [{'face_token': '3bb7e9482c7b512b9be7d553eeec178e',\n",
       "   'face_rectangle': {'top': 840, 'left': 519, 'width': 605, 'height': 605}}],\n",
       " 'image_id': 'ifvuZvI4KxZ0MIm4v+gtjg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key, API_sercret, 'LOK_01.JPG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e5983908",
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_key,API_sercret,'LOK_01.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5e770fdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'21a32fd1dbaec503756ac3c630aa8ddd'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens = face1['faces'][0]['face_token']\n",
    "face_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e2a6f973",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "30cdaabb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze - # coding: utf-8\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_sercret, face_tokens, landmark=1, attributes=None, face_rectangle=None, beauty_score_min=0, beauty_score_max=100)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    /Users/panyanfen/week_04人臉/analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8fbc8bf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 78,\n",
       " 'request_id': '1648565659,5261c56e-e30c-4f09-a2e3-1a84c9f9bba2',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 1219,\n",
       "     'x': 760},\n",
       "    'mouth_upper_lip_top': {'y': 1206, 'x': 824},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 1200, 'x': 798},\n",
       "    'left_eye_upper_left_quarter': {'y': 951, 'x': 674},\n",
       "    'left_eyebrow_lower_middle': {'y': 866, 'x': 696},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 1238, 'x': 778},\n",
       "    'right_eye_top': {'y': 959, 'x': 948},\n",
       "    'left_eye_bottom': {'y': 982, 'x': 702},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 878, 'x': 916},\n",
       "    'right_eye_pupil': {'y': 972, 'x': 951},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 1239, 'x': 873},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 1274, 'x': 863},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 1264, 'x': 894},\n",
       "    'contour_chin': {'y': 1446, 'x': 830},\n",
       "    'contour_left9': {'y': 1439, 'x': 754},\n",
       "    'left_eye_lower_right_quarter': {'y': 977, 'x': 732},\n",
       "    'mouth_lower_lip_top': {'y': 1237, 'x': 826},\n",
       "    'right_eyebrow_upper_middle': {'y': 855, 'x': 955},\n",
       "    'left_eyebrow_left_corner': {'y': 883, 'x': 616},\n",
       "    'right_eye_bottom': {'y': 987, 'x': 947},\n",
       "    'contour_left7': {'y': 1378, 'x': 629},\n",
       "    'contour_left6': {'y': 1324, 'x': 589},\n",
       "    'contour_left5': {'y': 1260, 'x': 566},\n",
       "    'contour_left4': {'y': 1192, 'x': 554},\n",
       "    'contour_left3': {'y': 1124, 'x': 547},\n",
       "    'contour_left2': {'y': 1056, 'x': 547},\n",
       "    'contour_left1': {'y': 989, 'x': 554},\n",
       "    'left_eye_lower_left_quarter': {'y': 977, 'x': 673},\n",
       "    'contour_right1': {'y': 997, 'x': 1100},\n",
       "    'contour_right3': {'y': 1126, 'x': 1102},\n",
       "    'contour_right2': {'y': 1061, 'x': 1104},\n",
       "    'mouth_left_corner': {'y': 1251, 'x': 734},\n",
       "    'contour_right4': {'y': 1192, 'x': 1093},\n",
       "    'contour_right7': {'y': 1375, 'x': 1022},\n",
       "    'right_eyebrow_left_corner': {'y': 869, 'x': 880},\n",
       "    'nose_right': {'y': 1123, 'x': 897},\n",
       "    'nose_tip': {'y': 1073, 'x': 828},\n",
       "    'contour_right5': {'y': 1257, 'x': 1080},\n",
       "    'nose_contour_lower_middle': {'y': 1139, 'x': 825},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 872, 'x': 656},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 1275, 'x': 790},\n",
       "    'right_eye_right_corner': {'y': 980, 'x': 997},\n",
       "    'right_eye_lower_right_quarter': {'y': 986, 'x': 974},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 1219, 'x': 890},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 890, 'x': 987},\n",
       "    'left_eye_left_corner': {'y': 965, 'x': 650},\n",
       "    'mouth_right_corner': {'y': 1251, 'x': 921},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 1238, 'x': 873},\n",
       "    'right_eye_lower_left_quarter': {'y': 982, 'x': 919},\n",
       "    'left_eyebrow_right_corner': {'y': 869, 'x': 782},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 868, 'x': 738},\n",
       "    'right_eye_center': {'y': 975, 'x': 947},\n",
       "    'nose_left': {'y': 1119, 'x': 749},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 1239, 'x': 780},\n",
       "    'left_eye_upper_right_quarter': {'y': 952, 'x': 734},\n",
       "    'right_eyebrow_lower_middle': {'y': 884, 'x': 952},\n",
       "    'left_eye_top': {'y': 946, 'x': 704},\n",
       "    'left_eye_center': {'y': 966, 'x': 704},\n",
       "    'contour_left8': {'y': 1416, 'x': 686},\n",
       "    'contour_right9': {'y': 1439, 'x': 905},\n",
       "    'right_eye_left_corner': {'y': 975, 'x': 894},\n",
       "    'mouth_lower_lip_bottom': {'y': 1278, 'x': 827},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 847, 'x': 649},\n",
       "    'left_eye_pupil': {'y': 960, 'x': 701},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 853, 'x': 915},\n",
       "    'contour_right8': {'y': 1415, 'x': 970},\n",
       "    'right_eyebrow_right_corner': {'y': 898, 'x': 1020},\n",
       "    'right_eye_upper_left_quarter': {'y': 961, 'x': 920},\n",
       "    'left_eyebrow_upper_middle': {'y': 835, 'x': 694},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 869, 'x': 992},\n",
       "    'nose_contour_left1': {'y': 969, 'x': 785},\n",
       "    'nose_contour_left2': {'y': 1068, 'x': 769},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 1200, 'x': 850},\n",
       "    'nose_contour_right1': {'y': 972, 'x': 864},\n",
       "    'nose_contour_right2': {'y': 1072, 'x': 882},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 1265, 'x': 760},\n",
       "    'contour_right6': {'y': 1320, 'x': 1058},\n",
       "    'nose_contour_right3': {'y': 1135, 'x': 862},\n",
       "    'nose_contour_left3': {'y': 1132, 'x': 785},\n",
       "    'left_eye_right_corner': {'y': 971, 'x': 760},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 841, 'x': 743},\n",
       "    'right_eye_upper_right_quarter': {'y': 966, 'x': 975},\n",
       "    'mouth_upper_lip_bottom': {'y': 1236, 'x': 825}},\n",
       "   'face_rectangle': {'width': 605, 'top': 840, 'left': 519, 'height': 605},\n",
       "   'face_token': '21a32fd1dbaec503756ac3c630aa8ddd'}]}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_sercret,face_tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4277b79d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648565660,c5f23d8b-533e-4627-9fc2-65710dc2f4f9',\n",
       " 'time_used': 316,\n",
       " 'faces': [{'face_token': '57e354353c26935c36eb0deab68ebd93',\n",
       "   'face_rectangle': {'top': 840, 'left': 519, 'width': 605, 'height': 605}}],\n",
       " 'image_id': 'ifvuZvI4KxZ0MIm4v+gtjg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key, API_sercret, 'LOK_01.JPG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cc643416",
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_key,API_sercret,'LOK_01.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "074ea81a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'62694c5cb37d52a94576cf6717a406d1'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token1 = face1['faces'][0]['face_token']\n",
    "face_token1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5e97cde5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648565661,d6f41409-7795-47bc-86e4-af09740e8628',\n",
       " 'time_used': 72,\n",
       " 'faces': [{'face_token': '1e4c39a0b324c6de404a0d1c6ccd9f23',\n",
       "   'face_rectangle': {'top': 164, 'left': 351, 'width': 361, 'height': 361}}],\n",
       " 'image_id': 'F44gf5SrX5zE7GZk/8fTiA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key, API_sercret, 'LOK_02.JPG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7a65a346",
   "metadata": {},
   "outputs": [],
   "source": [
    "face2 = detect.face_detect(API_key,API_sercret,'LOK_02.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5b600550",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'87fb9751676dfce605f001a29893c169'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token2 = face2['faces'][0]['face_token']\n",
    "face_token2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "81dadd91",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "19b33b3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare - # coding: utf-8\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_sercret, face_token1, face_token2)\n",
      "        该函数为调用face++ face_compare接口\n",
      "\n",
      "FILE\n",
      "    /Users/panyanfen/week_04人臉/compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f9674153",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648565662,0b9d4a65-3a39-4cc8-afcd-bf9cd2cdbadd',\n",
       " 'time_used': 339,\n",
       " 'confidence': 84.399,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_key,API_sercret,face_token1,face_token2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63db6d19",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85f8d6ee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c971a9f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
